﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Admin.Models;
using PagedList;
namespace OT.Areas.Admin.Controllers
{
    public class StateController : Controller
    {
        #region Declaration
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        int LanguageID = Utilities.GetLanguageID();
        #endregion

        #region Index
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, string searchString, int pgindex)
        {
            List<States> State = (from c in OTDb.States
                                  join sc in OTDb.Countries
                                  on c.CountryID equals sc.CountryID into output
                                  from j in output.DefaultIfEmpty()
                                  select new States
                                  {
                                      StateID = c.StateID,
                                      StateCode = c.StateCode,
                                      StateName = c.StateName,
                                      CreatedDate = (c.CreatedDate.HasValue) ? c.CreatedDate.Value : DateTime.MinValue,
                                      UpdatedDate = (c.UpdatedDate.HasValue) ? c.UpdatedDate.Value : DateTime.MinValue,
                                      CountryID = c.StateID,
                                      CountryName = j.CountryName
                                  }).OrderBy(o => o.StateName).ToList<States>();
            if (!string.IsNullOrEmpty(searchString))
                State = State.Where(r => r.StateName.ToLower().Contains(searchString.ToLower())).ToList();
            int recount = State.Count;
            State = State.Skip<States>(pgindex * (pageNumber - 1))
                .Take<States>(pgindex).ToList<States>();
            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = State,
                RecordCount = recount
            });
        }

        #endregion

        #region Create
        public ActionResult Create()
        {
            States Stat = null;
            try
            {
                Stat = new States();
                var coun = OTDb.Countries.ToList();
                Stat.Countrys = coun;
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(Stat);
        }
        [HttpPost]
        public string Create(string StateCode, string StateName, int CountryID)
        {
            string result = "";
            State State = null;
            try
            {
                if (ModelState.IsValid)
                {
                    State = new State();
                    State.StateCode = StateCode;
                    State.StateName = StateName;
                    State.CreatedDate = DateTime.Now;
                    State.UpdatedDate = DateTime.Now;
                    if (CountryID != 0)
                        State.CountryID = CountryID;
                    OTDb.States.Add(State);
                    OTDb.SaveChanges();
                    result = "Sucess";
                }
                else
                {
                    var Citys = OTDb.Countries.ToList();
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Edit
        public ActionResult Edit(int StateID)
        {
            States Stat = null;
            try
            {
                var categry = OTDb.States.Where(r => r.StateID == StateID).SingleOrDefault();
                if (categry != null)
                {
                    Stat = new States();
                    Stat.StateID = categry.StateID;
                    Stat.StateName = categry.StateName;
                    Stat.StateCode = categry.StateCode;
                    if (categry.StateID != 0)
                        Stat.CountryID = (int)categry.CountryID;
                    var category = OTDb.Countries.ToList();
                    Stat.Countrys = category;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(Stat);
        }

        [HttpPost]
        public string Edit(int StateID, string StateCode, string StateName, int CountryID)
        {
            string result = "";
            try
            {
                if (ModelState.IsValid)
                {
                    var State = OTDb.States.Where(r => r.StateID == StateID).SingleOrDefault();
                    if (State != null)
                    {
                        State.StateCode = StateCode;
                        State.StateName = StateName;
                        State.UpdatedDate = DateTime.Now;
                        if (StateID != 0)
                            State.CountryID = CountryID;
                        OTDb.SaveChanges();
                        result = "Sucess";
                    }
                }
                else
                {
                    var category = OTDb.Countries.ToList();
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Delete
        public ActionResult Delete(int StateID)
        {
            States State = null;
            try
            {
                var categry = OTDb.States.Where(r => r.StateID == StateID).SingleOrDefault();
                if (categry != null)
                {
                    State = new States();
                    State.StateID = categry.StateID;
                    State.StateName = categry.StateName;
                    State.StateCode = categry.StateCode;
                    if (categry.CountryID != null)
                        State.CountryID = (int)categry.CountryID;
                    var category = OTDb.Countries.ToList();
                    State.Countrys = category;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(State);
        }
        [HttpPost]
        public string Delete(int StateID, string IsDelete)
        {
            string result = "";
            try
            {
                var State = OTDb.States.Where(r => r.StateID == StateID).SingleOrDefault();
                if (State != null)
                {
                    OTDb.States.Remove(State);
                    OTDb.SaveChanges();
                    result = "Success";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion
    }
}
